//
//  UIImageView+WBWebCaChe.m
//  仿SDWebImage
//
//  Created by WangBiao on 16/9/18.
//  Copyright © 2016年 lsrain. All rights reserved.
//

#import "UIImageView+WBWebCaChe.h"
#import "DownloadManager.h"
#import <objc/runtime.h>


//需求：设置运行时关联对象--用于框架搭建
//需求：持有管理对象Blcok等待回调
@implementation UIImageView (WBWebCaChe)

- (void)WB_setImageWithURLString:(NSString *)URLString{
    
    //判断两次是不是同一个图片，如果是同一个，就把上一个取消
    if (![URLString isEqualToString:self.lastURL] && self.lastURL != nil) {
       //单例接管取消操作
        [[DownloadManager sharedManager] cancelOperationOfLastURL:self.lastURL];
    }
    //保存上一次的图片名
    self.lastURL = URLString;
    
    //单例接管下载
    [[DownloadManager sharedManager] downloadImgWithURLString:URLString andFinishBlock:^(UIImage *img) {
        
        //等待回调赋值
        self.image = img;
    }];

    
}

///分类执行使用运行时设置getter和setter
- (void)setLastURL:(NSString *)lastURL{
    objc_setAssociatedObject(self, "key", lastURL, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)lastURL{

    return objc_getAssociatedObject(self, "key");
}
@end
